/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */
package com.simon.mengkou.ui.fragment;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ExpandableListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshExpandableListView;
import com.ouertech.android.agm.lib.base.future.core.AgnettyFuture;
import com.ouertech.android.agm.lib.base.future.core.AgnettyResult;
import com.ouertech.android.agm.lib.base.utils.UtilList;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.Niudan;
import com.simon.mengkou.future.base.OuerFutureListener;
import com.simon.mengkou.system.constant.CstOuer;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.ui.adapter.ImagesExpandableAdapter;
import com.simon.mengkou.ui.base.BaseNiudanFragment;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/1/14.
 * desc    : 扭蛋图鉴
 */
public class NDImageFragment extends BaseNiudanFragment
            implements PullToRefreshBase.OnRefreshListener2<ExpandableListView>{
    //图鉴列表
    @Bind(R.id.image_id_expandable_list) PullToRefreshExpandableListView mPtrImages;


    private ImagesExpandableAdapter mAdapter;
    private int mPage = 0;

    @Override
    protected void initLayout() {
        setContentView(R.layout.fragment_image);
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this, getView());
        registerAction(CstOuer.BROADCAST_ACTION.NIUDAN_SEND_ACTION);
        setDoorVisible(false);

        //添加头部图片
        View viewHeader = LayoutInflater.from(mActivity)
                .inflate(R.layout.layout_image_header, null);
        mPtrImages.getRefreshableView().addHeaderView(viewHeader);
        mPtrImages.getRefreshableView().setGroupIndicator(null);

        mAdapter = new ImagesExpandableAdapter(mActivity);
        mPtrImages.getRefreshableView().setAdapter(mAdapter);
        mPtrImages.getRefreshableView().expandGroup(0, true);
        mPtrImages.setOnRefreshListener(this);
        mPtrImages.getLoadingLayoutProxy().setLoadingDrawable(null);

        getMineList(mPage);
    }


    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ExpandableListView> refreshView) {
        //下拉刷新
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ExpandableListView> refreshView) {
        //上拉加载更多
        getMineList(mPage);
    }


    /**
     * 获取我的扭蛋列表
     */
    private void getMineList(int page) {
        AgnettyFuture future = OuerApplication.mOuerFuture.getNiudanMineList(
                OuerApplication.mUser.getUserId(), page, CstOuer.PAGE.DEFAULT_SIZE,
                new OuerFutureListener(mActivity) {
                    @Override
                    public void onComplete(AgnettyResult result) {
                        super.onComplete(result);
                        List<Niudan> datas = (List<Niudan>) result.getAttach();
                        mPtrImages.onRefreshComplete();

                        if(UtilList.isNotEmpty(datas)) {
                            mAdapter.addList(datas);
                            mPage++;
                            if(UtilList.getCount(datas) < CstOuer.PAGE.DEFAULT_SIZE) {
                                mPtrImages.setMode(PullToRefreshBase.Mode.DISABLED);
                            }
                        } else {
                            mPtrImages.setMode(PullToRefreshBase.Mode.DISABLED);
                        }

                    }
                });

        attachDestroyFutures(future);
    }

    @Override
    public void onBackPressed() {
        mActivity.getSupportFragmentManager().popBackStack();
        setDoorVisible(true);
        openDoor(null);
    }

    @Override
    protected void onReceive(Intent intent) {
        super.onReceive(intent);
        if(CstOuer.BROADCAST_ACTION.NIUDAN_SEND_ACTION.equals(intent.getAction())) {
            mPage = 0;
            mAdapter.clearAll();
            mPtrImages.getRefreshableView().expandGroup(0, true);
            getMineList(mPage);
        }
    }
}
